package com.flycent.fanbao.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;

import com.flycent.fanbao.App;

public class NetworkStateService extends Service{

	private ConnectivityManager connectivityManager;
	private NetworkInfo info;

	private BroadcastReceiver mReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
				App.showLog("网络状态已经改变");
				connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
				info = connectivityManager.getActiveNetworkInfo();  
				if(info != null && info.isAvailable()) {
					String name = info.getTypeName();
					App.showLog("当前网络名称：" + name);
//					App.showShortToast("网络已连接");
					
//					doSomething()
					
				} else {
					App.showLog("没有可用网络");
					App.showShortToast("当前没有可用网络");
					//doSomething()
				}
			}
		}
	};

	@Override
	public IBinder onBind(Intent intent) {
		App.showLog("NetworkStateService--onBind");
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		App.showLog("NetworkStateService--onCreate");
		IntentFilter mFilter = new IntentFilter();
		mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		registerReceiver(mReceiver, mFilter);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		App.showLog("NetworkStateService--onDestroy");
		unregisterReceiver(mReceiver);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}

}
